package com.dykj.fireFighting.server.elasticsearch.devHisGas.service.impl;

import com.dykj.fireFighting.api.elasticsearch.devHisGas.entity.EsDevHisGas;
import com.dykj.fireFighting.api.elasticsearch.devHisGas.service.EsDevHisGasService;
import com.dykj.fireFighting.api.elasticsearch.devHisSomke.entity.EsDevHisSomke;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.server.elasticsearch.devHisGas.repository.EsDevHisGasRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@DubboService
public class EsDevHisGasServiceImpl implements EsDevHisGasService {
    @Autowired
    private EsDevHisGasRepository esDevHisGasRepository;
    @Override
    public EsDevHisGas importAll(EsDevHisGas esDevHisGas) {
        return esDevHisGasRepository.save(esDevHisGas);
    }

    @Override
    public PageVo<EsDevHisGas> searchByCenterId(String centerId, String unitId, String deviceName, Integer pageNum, Integer pageSize, String beginDate, String endDate) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withPageable(pageable);
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if (StringUtils.isNotBlank(centerId)) {
            boolQuery.must(QueryBuilders.termQuery("centerUnitId", centerId));
        }
        if (StringUtils.isNotBlank(unitId)) {
            boolQuery.must(QueryBuilders.termQuery("unitId", unitId));
        }
        if (StringUtils.isNotBlank(deviceName)) {
            boolQuery.must(QueryBuilders.wildcardQuery("deviceName", "*" + deviceName + "*"));
        }
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
            boolQuery.must(QueryBuilders.rangeQuery("uploadTime")
                    .from(beginDate)
                    .to(endDate)
                    .includeLower(true)     // 包含上界
                    .includeUpper(true));      // 包含下届
        }
        nativeSearchQueryBuilder.withQuery(boolQuery);
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("uploadTime").order(SortOrder.DESC));
        NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
        Page<EsDevHisGas> search = esDevHisGasRepository.search(searchQuery);
        PageVo<EsDevHisGas> page = new PageVo<>();
        page.setTotalCount((int) search.getTotalElements());
        List<EsDevHisGas> paggList = new ArrayList<>();
        for (EsDevHisGas e : search.getContent()){
            paggList.add(e);
        }
        page.setList(paggList);
        return page;
    }
}
